package edu.lingnan.librarymanage.service.impl;

import edu.lingnan.librarymanage.config.redis.RedisCache;
import edu.lingnan.librarymanage.core.bean.Category;
import edu.lingnan.librarymanage.core.constant.RedisConstants;
import edu.lingnan.librarymanage.core.dto.CategoryDTO;
import edu.lingnan.librarymanage.core.exception.CategoryException;
import edu.lingnan.librarymanage.core.vo.CategoryVO;
import edu.lingnan.librarymanage.repository.CategoryRepository;
import edu.lingnan.librarymanage.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cql
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private CategoryRepository categoryRepository;

    @Override
    public List<CategoryVO> findAllCategory() {
        List<CategoryVO> result = redisCache.getCacheList(RedisConstants.CATEGORY);
        if (result == null || result.size() == 0) {
            List<Category> categories = categoryRepository.findAll();
            result = categories.stream().map(c -> {
                CategoryVO vo = new CategoryVO();
                BeanUtils.copyProperties(c, vo);
                return vo;
            }).collect(Collectors.toList());
            redisCache.setCacheList(RedisConstants.CATEGORY, result);
        }
        return result;
    }

    @Override
    public CategoryVO insertCategory(CategoryDTO category) {
        /**
         * 查询是否有同样名字的
         */
        Category c = categoryRepository.findByName(category.getName());
        if (c != null) {
            return null;
        }

        Category bean = new Category();
        BeanUtils.copyProperties(category, bean);

        bean = categoryRepository.save(bean);
        CategoryVO result = new CategoryVO();
        BeanUtils.copyProperties(bean, result);

        /**
         * 删除缓存
         */
        redisCache.deleteObject(RedisConstants.CATEGORY);

        return result;
    }

    @Override
    public boolean delateCategory(Integer categoryId) {
        List<Category> categories = categoryRepository.findByParent(categoryId);
        if (categories != null && categories.size() > 0) {
            return false;
        }
        categoryRepository.deleteById(categoryId);

        /**
         * 删除缓存
         */
        redisCache.deleteObject(RedisConstants.CATEGORY);

        return true;
    }

    @Override
    public void updateCategory(CategoryDTO category) {
        if (category == null || category.getCategoryId() == null || category.getName() == null || "".equals(category.getName().trim())) {
            throw new CategoryException("系统繁忙，请稍后再试");
        }
        /**
         * 查询是否有同样名字的
         */
        Category c = categoryRepository.findByName(category.getName());
        if (c != null) {
            throw new CategoryException("分类名称重复,请重新设置");
        }

        categoryRepository.updateCategory(category.getCategoryId(), category.getName());

        /**
         * 删除缓存
         */
        redisCache.deleteObject(RedisConstants.CATEGORY);

    }
}
